package com.phone.servlet;

import com.phone.ParameterCheck;
import com.phone.entity.APP;
import com.phone.handle.AppHandle;
import com.phone.handle.EntityHandle;
import org.hibernate.Session;
import org.json.JSONObject;

import javax.servlet.annotation.WebServlet;
import java.util.Date;

/**
 * Created by tisen on 2017/9/5.
 */
@WebServlet(name = "App", urlPatterns = "/app")
public class AppServlet extends EntityServlet<APP> {


    private static final String NAME = "name";
    private static final String INTRODUCTION = "introduction";
    private static final String ICON_URL = "icon_url";
    private static final String FILE_URL = "file_url";
    private static final String VERSION = "version";


    @Override
    protected JSONObject otherHandle(String action, ParameterCheck check, Session session) throws Exception {
        return null;
    }

    @Override
    public APP getEntity(ParameterCheck check) throws Exception {
        APP app = new APP();
        app.setName(check.getParameterString(NAME, false));
        app.setIntroduction(check.getParameterString(INTRODUCTION, true));
        app.setIcon_url(check.getParameterString(ICON_URL, false));
        app.setFile_url(check.getParameterString(FILE_URL, false));
        app.setVersion(check.getParameterString(VERSION, false));
        app.setUpdate_time(new Date());
        return app;
    }

    @Override
    public APP getUpdateEntity(ParameterCheck check) throws Exception {
        String id = check.getParameterString(ID, false);
        APP app = session.get(APP.class, id);
        return app;
    }

    @Override
    public EntityHandle getHandle(Session session) {
        return new AppHandle(session);
    }

    @Override
    public String getSearch(ParameterCheck check) throws Exception {
        return "from APP";
    }

    @Override
    public String getOwnerID(ParameterCheck check) throws Exception {
        return null;
    }
}
